import { getMenuList } from "@/api/cate"
import {message} from "ant-design-vue";
import {encryData} from '@/utils/utils'


const menu = {
    namespaced:true,
    state:{
        // 预设菜单信息
        menuList:[
            {
                title:'首页',
                link:'/'
            },{
                title:'分类',
                link:'/category',
            },{
                title:'归档',
                link:'/archive'
            },{
                title:'标签页',
                link:'/tag'
            },{
                title:'友链',
                link:'/friend'
            },{
                title:'留言',
                link:'/message'
            }
        ],
        // 抽屉状态
        isOpen:false
    },
    mutations:{
        // 修改路由添加上子路由分类
        SET_MENU_LIST(state:any,data:Array<any>){
            let _menuList = state.menuList
            let findResult = _menuList.find((item:any) => item.link == '/category')
            let childCates:any = []
            data.forEach((item:any)=>{
                item.cateId = encryData(item.cateId)
                item["link"] = `/category/${item.cateId}`
                item["title"] = item.cateName
                childCates.push(item)
            })
            findResult["childCates"] = childCates
            state.menuList = _menuList
            // console.log('修改后的菜单列表',state.menuList);
        },
        SET_OPEN_STATUS(state:any,openStatus:boolean){
            state.isOpen = openStatus
        }
    },
    actions:{
        // 修改当前路由 添加对应的子路由
        async CHANGE_ROUTER(context:any,data:any){
            try {
                let res:any = await getMenuList()
                // console.log('获取到的分类列表',res);
                context.commit('SET_MENU_LIST',res.cates)
            } catch (error:any) {
                message.error(error.message)
            }
        }
    }
}

export default menu
